package com.edgclub.edg.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.edgclub.edg.R;
import com.edgclub.edg.adapter.GroupAdapter2;
import com.edgclub.edg.adapter.adapteruitls.MyItemClickListener;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.ResultCode;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.http.VolleyListener;
import com.edgclub.edg.model.GroupBean;
import com.edgclub.edg.model.result.GroupListData;
import com.edgclub.edg.utils.util.JSONHelper;
import com.edgclub.edg.utils.util.JsonUtil;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;
import java.util.List;


/**
 */
public class GzAllGroupListActivity extends GzBaseTooolBarActivity implements
        OnClickListener ,VolleyListener{

    private XRecyclerView recyclerView;
    private int currPage = 1;
    private int pageSize = 20;
    private GroupAdapter2 groupAdapter;
    private UserPrefUtils userPrefUtils;
    private List<GroupBean> groupBeanList;
    private FriendApi friendApi;
    @Override
    public int getContentView() {
        return  R.layout.activity_gz_game_list_all;
    }
    @Override
    public void setTitleBarView() {
        setCustomActionBar();
        title.setText("所有群组");
        main_back.setVisibility(View.VISIBLE);
        main_back.setOnClickListener(this);
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).register(this);
    }
    @Override
    public void initView() {
        recyclerView= (XRecyclerView) findViewById(R.id.recyclerview);
    }
    @Override
    public void initData() {
        showDialog1();
        userPrefUtils = new UserPrefUtils(this);
        groupAdapter= new GroupAdapter2(this);
        friendApi = new FriendApi(this);
        groupBeanList=new ArrayList<>();
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        groupAdapter.setDatas(groupBeanList);
        groupAdapter.setOnItemClickListener(new MyItemClickListener() {
            @Override
            public void onItemClick(View view, int postion) {
                Intent intent=new Intent(GzAllGroupListActivity.this, GroupDetailsActivity.class);
                intent.putExtra("groupid",groupBeanList.get(postion).getGroupid());
                intent.putExtra("groupname",groupBeanList.get(postion).getGname());
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(groupAdapter);
        recyclerView.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                currPage = 1;
                getGroupPost(currPage);
            }

            @Override
            public void onLoadMore() {
                getGroupPost(currPage);
            }
        });
        getGroupPost(currPage);
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).unregister(this);
    }


    private void getGroupPost(int page) {
        MessageParam myParam = new MessageParam();
        myParam.setPage(page + "");
        myParam.setPageSize(pageSize + "");
        myParam.setUserId(userPrefUtils.getUser().getId() + "");
        friendApi.getAllGroupList(myParam,this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            default:
                break;
            case R.id.main_back:
                finish();
                break;
        }
    }


    @Override
    public void onStart(String tag) {

    }

    @Override
    public void onSuccess(String response, String tag) {
        dismissDialog();
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        if (JSONHelper.isSuccess(response)) {
            GroupListData data = JsonUtil.getMode(response,
                    GroupListData.class);
            if(null!=data.getData() && data.getData().size()>0) {
                if (currPage == 1) {
                    groupBeanList.clear();
                }
                currPage++;
                groupBeanList.addAll(data.getData());
                groupAdapter.notifyDataSetChanged();
            }else{
                recyclerView.noMoreLoading();
            }
        }
    }

    @Override
    public void onFaile(VolleyError volleyError, String tag) {
        recyclerView.refreshComplete();
        recyclerView.loadMoreComplete();
        setNoNetWork(new ReLoadClickListener() {
            @Override
            public void reload() {
                getGroupPost(currPage);
            }
        });
        ResultCode.toastVolleyError(this, volleyError);
    }
}
